﻿namespace TiaoTiaoCode.Common.Extensions
{
    /// <summary>
    /// 数字类扩展
    /// </summary>
    public static class DigitEx
    {
		/// <summary>
		/// 将整数进行有效的拆分成数组，指定每个元素的最大值<br />
		/// Effectively split integers into arrays, specifying the maximum value for each element
		/// </summary>
		/// <param name="integer">整数信息</param>
		/// <param name="everyLength">单个的数组长度</param>
		/// <returns>拆分后的数组长度</returns>
		/// <example>
		/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftBasicExample.cs" region="SplitIntegerToArrayExample" title="SplitIntegerToArray示例" />
		/// </example>
		public static int[] TiaoTiaoSplitIntegerToArray(this int integer, int everyLength)
		{
			int[] result = new int[(integer / everyLength) + ((integer % everyLength) == 0 ? 0 : 1)];
			for (int i = 0; i < result.Length; i++)
			{
				if (i == result.Length - 1)
				{
					result[i] = (integer % everyLength) == 0 ? everyLength : (integer % everyLength);
				}
				else
				{
					result[i] = everyLength;
				}
			}
			return result;
		}

		public static int[] TiaoTiaoSplitIntegerToArray(this ushort integer, int everyLength) => TiaoTiaoSplitIntegerToArray((int)integer, everyLength);
	}
}
